package com.taxi.util;

import cn.hutool.core.util.StrUtil;
import org.geolatte.geom.Point;
import org.geolatte.geom.codec.Wkt;

import java.math.BigDecimal;
import java.util.Arrays;

public abstract class GeoPointUtil {

    public static Point pt(double x, double y) {
        String str = StrUtil.format("point({} {})", x, y);
        return (Point) Wkt.fromWkt(str);
    }

    public static Point point(String location){
        String[] array = location.split(",");
        String str = StrUtil.format("point({} {})", array[0], array[1]);
        return (Point) Wkt.fromWkt(str);
    }

    public static String str(Point p) {
        if (null == p) {
            return "";
        }
        double[] array = {};
        array = p.getPosition().toArray(array);
        return array[0] + "," + array[1];
    }

    public static String str(double x, double y) {
        String str = StrUtil.format("{},{}", x, y);
        return str;
    }

    public static String toBaiduStr(Point p) {
        if (null == p) {
            return "";
        }
        double[] array = {};
        array = p.getPosition().toArray(array);
        return array[1] + "," + array[0];
    }

    public static double getX(Point p) {
        if (null == p) {
            return 0;
        }
        double[] array = {};
        array = p.getPosition().toArray(array);
        return Double.valueOf(array[0]);
    }

    public static double getY(Point p) {
        if (null == p) {
            return 0;
        }
        double[] array = {};
        array = p.getPosition().toArray(array);
        return Double.valueOf(array[1]);
    }

    public static double toKmPerHourSpeed(double speed) {
        if (speed <= 0) return 0;
        return BigDecimal.valueOf(speed).multiply(BigDecimal.valueOf(3600)).divide(BigDecimal.valueOf(1000), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double[] getPoints(String point) {
        return Arrays.stream(point.split(",")).mapToDouble(Double::new).toArray();
    }

}